package wei.xinya;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerRecord;

import java.util.Date;
import java.util.Properties;

public class Producer {
    public static void main(String[] args) {
        Properties props = new Properties();
        props.put("bootstrap.servers", "192.168.77.50:9092");
        props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
        props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");

        try (KafkaProducer<Object, Object> producer = new KafkaProducer<>(props)) {
            String topic = "RTW_DWFN_TEZHEN_GZZECZA";
            String key = "key";

            JSONObject mes = new JSONObject();
            mes.put("JOBTYPE", "rtw");
            mes.put("JOBID", "123456");
            mes.put("JOBNAME", "重点人员测试任务");
            mes.put("DISTRICT", "510000");
            mes.put("POLICE", "JD");

            String dataStr = "{\"UUID\":\"20230330081148UUUUUUUUU\",\"TASKID\":\"1698903931211\",\"WARNING_TIME\":\"2024-04-26 15:45:48\",\"TARGET\":\"460002081759139\",\"MSISDN\":\"460002081759133\",\"HOMEAREA\":\"028\",\"CURAREA\":\"028\",\"RELATENUM\":\"13655491886\",\"RELATEHOMEAC\":\"028\",\"LAI\":\"6522\",\"CI\":\"556645\",\"LONGITUDE\":\"106.929079\",\"LATITUDE\":\"30.660069\",\"SENDTIME\":\"2023-11-06 15:45:48\",\"RESULTDESC\":\"离开四川达州（正常离开）,到达四川广安，触发离开城市预警；\",\"SUBALARMTYPE\":\"YJ_MB_LKCS\"}";
            JSONObject dataObj = JSONObject.parseObject(dataStr);

            for (int i = 0; i < 100; i++) {
                String curDate = DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss");
                dataObj.put("WARNING_TIME", curDate);
                dataObj.put("SENDTIME", curDate);
                dataObj.put("TARGET", new Date().getTime() + "");
                mes.put("DATA", dataObj.toJSONString());

                ProducerRecord<Object, Object> record = new ProducerRecord<>(topic, key + "-" + i, mes.toJSONString());
                System.out.println(record);
                producer.send(record);
                Thread.sleep(3000);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
